package com.quanyan.riding.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.riding.entity.db.*;
import com.quanyan.riding.entity.vo.RidingFootPrint;
import com.quanyan.riding.entity.vo.RidingPathUser;
import com.quanyan.riding.entity.vo.req.ReqRidingFootPrint;
import com.quanyan.riding.entity.vo.req.ReqRidingPathUser;
import com.quanyan.riding.entity.vo.resp.RespRidingPathUser;
import com.quanyan.riding.mapper.MyTbRidingRecordMapper;
import com.quanyan.riding.mapper.TbRidingFootprintMapper;
import com.quanyan.riding.mapper.TbRidingPathMapper;
import com.quanyan.riding.mapper.TbRidingPathUserMapper;
import com.quanyan.riding.service.RidingPathService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xian-guo
 * @email 498152332@qq.com
 * @date 2016/8/31.
 */
@Service("ridingPathService")
public class RidingPathServiceImpl implements RidingPathService {

    private static final Logger LOGGER = LoggerFactory.getLogger(RidingPathServiceImpl.class);

    /**
     * 路书路线
     */
    @Autowired
    private TbRidingPathMapper tbRidingPathMapper;

    /**
     * 路书上用户
     */
    @Autowired
    private TbRidingPathUserMapper tbRidingPathUserMapper;

    /**
     * 自定义查询
     */
    @Autowired
    private MyTbRidingRecordMapper myTbRidingRecordMapper;

    /**
     * 路书足迹
     */
    @Autowired
    private TbRidingFootprintMapper tbRidingFootprintMapper;

    /**
     * 查找城市所有的路书路径
     * 客户端判断如果用户位置在路书上，就传用户位置给服务器，如果不在就不传
     * 获取路书路线
     * @param cityId
     * @return
     */
    @Override
    public APIResponse<List<TbRidingPath>> getRidingPathMessage(int cityId) {
        TbRidingPathExample tbRidingPathExample=new TbRidingPathExample();
        tbRidingPathExample.createCriteria().andCityIdEqualTo((byte) cityId);
        List<TbRidingPath> tbRidingPathList=tbRidingPathMapper.selectByExample(tbRidingPathExample);
        //查找路径,现在只有一条路线，后续版本迭代添加多条路书需要改
        List<TbRidingPath> tbRidingPathBlob=tbRidingPathMapper.selectByExampleWithBLOBs(tbRidingPathExample);
        tbRidingPathList.get(0).setLongLat(tbRidingPathBlob.get(0).getLongLat());
        return APIResponse.returnSuccess(tbRidingPathList);
    }

    /**
     * 进入路书页面查询路书上用户和足迹信息
     * @param reqRidingPathUser
     * @return
     */
    @Override
    public APIResponse<RespRidingPathUser> getRidingPathUserPrint(ReqRidingPathUser reqRidingPathUser) {
        RespRidingPathUser respRidingPathUser=new RespRidingPathUser();
        //判断该用户是否在路书上，客户端给了用户位置点表示用户在路书上，否则不在
        String longLat=reqRidingPathUser.getLongLat();
        if(!StringUtil.isBlankOrNull(longLat)){//当前用户在路书上
            //查询该用户在路书这个圈里面是否来过
            TbRidingPathUserExample tbRidingPathUserExample=new TbRidingPathUserExample();
            tbRidingPathUserExample.createCriteria().andUserIdEqualTo(reqRidingPathUser.getUserId()).andPathIdEqualTo(reqRidingPathUser.getId());
            List<TbRidingPathUser> tbRidingPathUserList=tbRidingPathUserMapper.selectByExample(tbRidingPathUserExample);
            int succ=0;
            if(tbRidingPathUserList.size()>0){//该用户24内进入过该路书圈
                TbRidingPathUser tbRidingPathUserUp=tbRidingPathUserList.get(0);
                tbRidingPathUserUp.setLongLat(reqRidingPathUser.getLongLat());//更新经纬度
                tbRidingPathUserUp.setCreateTime(DateUtils.tranferStringToDate(reqRidingPathUser.getCreateTime()));//更新时间
                succ=tbRidingPathUserMapper.updateByExample(tbRidingPathUserUp,tbRidingPathUserExample);
                succ++;
            }else{//该路书上24内用户没来过
                TbRidingPathUser tbRidingPathUser=new TbRidingPathUser();
                tbRidingPathUser.setPathId(reqRidingPathUser.getId());
                tbRidingPathUser.setUserId(reqRidingPathUser.getUserId());
                tbRidingPathUser.setLongLat(reqRidingPathUser.getLongLat());
                tbRidingPathUser.setCreateTime(DateUtils.tranferStringToDate(reqRidingPathUser.getCreateTime()));
                succ=tbRidingPathUserMapper.insert(tbRidingPathUser);
            }
            LOGGER.debug("跟新(2)或插入用户(1)----------->"+succ);
        }
        //根据路书编号，查询路书上所有用户
        List<RidingPathUser> ridingPathUserList =myTbRidingRecordMapper.getRidingPathUserMsg(reqRidingPathUser.getId());
        LOGGER.debug("该路书上的用户人数------------->"+ridingPathUserList.size());
        //路书上的人数
        respRidingPathUser.setRidingPeopeNum(ridingPathUserList.size());
        //路书上所有用户信息
        respRidingPathUser.setTbRidingUserList(ridingPathUserList);
        //根据路书编号，查询路书上所有用户足迹的详细信息
        List<RidingFootPrint> ridingFootPrintList=myTbRidingRecordMapper.getRidingFootPrintUserMsg(reqRidingPathUser.getId());
        respRidingPathUser.setRidingFootPrintList(ridingFootPrintList);
        return APIResponse.returnSuccess(respRidingPathUser);
    }

    /**
     * 上传用户足迹
     * @param reqRidingFootPrint
     * @return
     */
    @Override
    public APIResponse insertRidingFootPrintMsg(ReqRidingFootPrint reqRidingFootPrint) {
        TbRidingFootprint tbRidingFootprint=new TbRidingFootprint();
        tbRidingFootprint.setUserId(reqRidingFootPrint.getUserId());
        tbRidingFootprint.setPathId(reqRidingFootPrint.getPathId());
        tbRidingFootprint.setLongLat(reqRidingFootPrint.getLongLat());
        tbRidingFootprint.setFootUrl(reqRidingFootPrint.getPhotoUrl());
        tbRidingFootprint.setFootprintDesc(reqRidingFootPrint.getFootPrintDesc());
        int succInsert=tbRidingFootprintMapper.insert(tbRidingFootprint);
        LOGGER.debug("插入足迹是否成功------------------------------>"+succInsert);
        return succInsert>0?APIResponse.returnSuccess():APIResponse.returnFail("插入足迹失败");
    }
}
